package com.example.activiti.demo.controller;

import com.example.activiti.demo.entity.SysRole;
import com.example.activiti.demo.entity.SysUser;
import com.example.activiti.demo.service.SysRoleService;
import com.example.activiti.demo.service.SysUserService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.User;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: cheng
 * @date: 2020/2/20 18:50
 */
@Controller
@RequestMapping("/my/test")
public class MytestController {

    @Resource
    protected SysRoleService sysRoleService;

    @Resource
    protected SysUserService sysUserService;

    @Autowired
    protected RuntimeService runtimeService;


    @Autowired
    protected IdentityService identityService;

    @Autowired
    protected RepositoryService repositoryService;

    @Autowired
    protected TaskService taskService;

    @GetMapping("/deploy")
    public void policeBegin(HttpServletRequest request,
                            HttpServletResponse response) {


        DeploymentBuilder deploymentBuilder = repositoryService
                .createDeployment()
                .category("MyTest")
                .name("MyTest")
                .addClasspathResource("process/MyTest.bpmn");
        Deployment deploy = deploymentBuilder.deploy();

        System.out.println("流程ID: " + deploy.getId());

        String processDefinitionKey = "MyTest";
        Map<String, Object> vars = new HashMap<String, Object>();
        runtimeService.startProcessInstanceByKey(processDefinitionKey, vars);
    }

    @GetMapping("/taskList")
    public void taskList(HttpServletRequest request,
                         HttpServletResponse response) {

        List<Task> taskList = taskService.createTaskQuery()
                .processDefinitionKey("MyTest")
                .list();

        for (Task task : taskList
        ) {

            System.out.println(task.getName());
        }

    }


    @GetMapping("/zhangsan")
    public void zhangsan(HttpServletRequest request,
                         HttpServletResponse response) {

        List<Task> taskList = taskService.createTaskQuery()
                .processDefinitionKey("MyTest")
                .taskAssignee("张三")
                .list();

        for (Task task : taskList
        ) {
            if ("张三".equals(task.getAssignee())) {
                taskService.complete(task.getId());
            }
            System.out.println(task.getName());
        }

    }

    @GetMapping("/lisi")
    public void lisi(HttpServletRequest request,
                     HttpServletResponse response) {
        List<Task> taskList = taskService.createTaskQuery()
                .processDefinitionKey("MyTest")
                .taskAssignee("李四")
                .list();

        for (Task task : taskList
        ) {
            if ("李四".equals(task.getAssignee())) {
                taskService.complete(task.getId());
            }
            System.out.println(task.getName());
        }
    }


    @GetMapping("/roleAdd")
    public void roleAdd(HttpServletRequest request,
                        HttpServletResponse response) {
        SysRole sysRole = new SysRole();
        sysRole.setId("1");
        sysRole.setRoleName("员工");
        sysRoleService.saveOrUpdate(sysRole);
        sysRole.setId("2");
        sysRole.setRoleName("经理");
        sysRoleService.saveOrUpdate(sysRole);
    }

    @GetMapping("/userAdd")
    public void userAdd(HttpServletRequest request,
                        HttpServletResponse response) {
        SysUser sysUser = new SysUser();
        sysUser.setId("1");
        sysUser.setRoleId("1");
        sysUser.setUsername("zhangsan");
        sysUser.setRealName("张三");
        sysUser.setPassword("zhangsan");
        sysUserService.saveOrUpdate(sysUser);
        sysUser.setId("2");
        sysUser.setRoleId("2");
        sysUser.setUsername("lisi");
        sysUser.setRealName("李四");
        sysUser.setPassword("lisi");
        sysUserService.saveOrUpdate(sysUser);
    }


    @GetMapping("/getUser")
    public void getUser(HttpServletRequest request,
                        HttpServletResponse response) {
        List<User> list = identityService.createNativeUserQuery().list();
        identityService.createNativeGroupQuery().list();
        identityService.createUserQuery().userId("1").singleResult();
        //List<Group> list = identityService.createGroupQuery().groupId("1").list();
        System.out.println(list);
        // System.out.println(list);


    }


}
